package com.lz.wsy.oss2;

import com.alibaba.druid.wall.violation.ErrorCode;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;

import com.aliyun.oss.OSSClientBuilder;
import com.lz.utils.Result;
import com.lz.wsy.pojo.OssProperties;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 这是一个类
 *
 * @author wzy
 * @date 2021/11/18  15:53
 */
@Component("uploadService")
public class OssUtils{

    @Autowired
    private OssProperties ossProperties;

    public static String[] imgs = {"jpg", "png", "gif", "jpeg","pdf"};
    public static String[] videos = {"mp4", "avi", "mp3", "wmv", "mpg", "mpeg", "mov", "rm", "swf", "flv", "ram"};
    public static String[] files = {"doc", "docx", "xls", "xlsx", "txt", "ppt", "pptx"};


    public  Result uploadFile(MultipartFile file) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        OSSClient ossClient = null;
        URL url = null;
        String filename = null;
        try {
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀
            String extension = FilenameUtils.getExtension(originalFilename);
            //更改文件名字
            String newName = UUID.randomUUID().toString().toUpperCase().replace("-", "");
            filename = newName + "." + extension;
            String objectName = null;
            if (ArrayUtils.contains(imgs, extension)) {
                objectName = "productManual" + "/img/" + simpleDateFormat.format(new Date()) + "/" + filename;
            } else if (ArrayUtils.contains(videos, extension)) {
                objectName = "productManual" + "/video/" + simpleDateFormat.format(new Date()) + "/" + filename;
            } else if (ArrayUtils.contains(files, extension)) {
                objectName = "productManual" + "/file/" + simpleDateFormat.format(new Date()) + "/" + filename;
            } else {
                return Result.error("此类型的文件不可上传");
            }

            //创建连接
            ossClient = new OSSClient(ossProperties.getEndpoint(),
                    ossProperties.getKeyid(),
                    ossProperties.getKeysecret());

            ossClient.putObject(ossProperties.getBucketname(), objectName, file.getInputStream());
            //设置URL存活时间
            final Long liveTime = 3600L * 1000 * 24 * 365 * 10;
            Date expiration = new Date(System.currentTimeMillis() + liveTime);
            url = ossClient.generatePresignedUrl(ossProperties.getBucketname(), objectName, expiration);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        String toStringUrl = url.toString();
        Image image = new Image();
        image.setSrc(toStringUrl);
        image.setTitle(filename);
        return Result.success(image);
    }
}

